# -*- coding: utf-8 -*-
import logging
from selenium.common.exceptions import TimeoutException, NoSuchElementException

from pages.base_page import BasePage
from objectsmap.page_locators import Contact
from pages_exceptions.pages_exceptions import ContactException


class ContactPage(BasePage):
    
    TITLE = 'Contact one of our IASO offices directly.'
    LOCATION = 'https://www.iaso.com/contact-us'

    def __init__(self, driver):
        super(ContactPage, self).__init__(driver, ContactPage.LOCATION)

    def is_right_location(self):
        super(ContactPage, self).is_right_location(ContactPage.TITLE)

    def contact(self, contact_us):
        try:
            name_element = self._driver.find_element(*Contact.IN_NAME)
            name_element.send_keys(contact_us['name'])

            email_element = self._driver.find_element(*Contact.IN_EMAIL)
            email_element.send_keys(contact_us['email'])

            phone_element = self._driver.find_element(*Contact.IN_PHONE)
            phone_element.send_keys(contact_us['phone'])

            message_element = self._driver.find_element(*Contact.IN_MESSAGE)
            message_element.send_keys(contact_us['message'])

            self._driver.find_element(*Contact.BTN_SUBMIT).submit()
            logging.info('contact us success')
        except (TimeoutException, NoSuchElementException) as e:
            raise ContactException('Can\'t contact')
